import { Component } from '@angular/core';

import { Http } from '@angular/http';

import { AlertsService } from '../../modules/ng2-alerts';

import { MatSnackBar } from '@angular/material';


@Component({
  selector: 'app-custom',
  templateUrl: './custom.page.html',
  styles: [`
  .custom-box{
    margin: 0 auto;
    max-width:600px;
    width:100%;
  }
  .custom-content{
    width:100%;
    height:300px;
  }
  `]
})

export class CustomPage {

  error: boolean = true;

  vm: any = {
    mobile: "",
    qq: "",
    msg: ""
  };

  constructor(private http: Http, private alert: AlertsService, public snackBar: MatSnackBar) {
    // code...
  }

  send() {

    if (this.vm.qq == '' && this.vm.mobile == '') {

      // Simple message.
      this.snackBar.open('请您任选一个联系方式!','', {
        duration: 3000
      });

      // Simple message with an action.
      // this.snackBar.open('Message archived', 'Undo');
      // this.alert.create('info', "请您任选一个联系方式!", { duration: 1500 });
      return;
    }

    if (this.vm.mobile != '' && !(/^1[34578]\d{9}$/.test(this.vm.mobile))) {
      this.snackBar.open('手机号码格式错误!','', {
        duration: 3000
      });
      // this.alert.create('info', "手机号码格式错误", { duration: 1500 });
      return;
    }



    if (this.vm.msg == "") {
      this.snackBar.open('请填写您的定制需求!','', {
        duration: 3000
      });
      // this.alert.create('info', "请填写您的定制需求", { duration: 1500 });
      return;
    }

    /*let json:any = {
      msg:this.vm.msg
    };

    if(this.vm.mobile != ''){
      json.mobile = this.vm.mobile;
    }

    if(this.vm.qq != ''){
      json.qq = this.vm.qq;
    }*/


    this.http.post('/qbmf/api/otherInfo/addDataOrder', {
      content: this.vm.msg,
      phone: this.vm.mobile,
      qq: this.vm.qq
    }).subscribe(response => {
      console.log(response);

      this.alert.create('success', '发送成功', { duration: 2000 });
      this.vm.mobile = '';
      this.vm.qq = '';
      this.vm.msg = '';

    }, error => {
      // console.log('hhhhhhhhhhhhh');
      console.log(error);

    });


  }
}
